package com.spider.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spider.usercenter.common.BaseResponse;
import com.spider.usercenter.common.ErrorCode;
import com.spider.usercenter.common.ResultUtils;
import com.spider.usercenter.contant.UserConstant;
import com.spider.usercenter.exception.BussinessException;
import com.spider.usercenter.model.domain.User;
import com.spider.usercenter.model.domain.request.UserLoginRequest;
import com.spider.usercenter.model.domain.request.UserRegisterRequest;
import com.spider.usercenter.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 注册
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkUserPassword = userRegisterRequest.getCheckPassword();
        String planetCode = userRegisterRequest.getPlanetCode();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkUserPassword,planetCode)) {
            throw new BussinessException(ErrorCode.NULL_ERROR);
        }

        long result=userService.userRegister(userAccount, userPassword, checkUserPassword,planetCode);
        return ResultUtils.success(result);
    }

    /**
     * 登录
     * @param userLoginRequest
     * @param request
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();

        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BussinessException(ErrorCode.NULL_ERROR);
        }
       User user = userService.userLogin(userAccount, userPassword, request);
        return ResultUtils.success(user);
    }
    @PostMapping("/logout")
    public BaseResponse<Integer>userLout( HttpServletRequest request) {

        if (request==null) {
            throw new BussinessException(ErrorCode.NULL_ERROR);
        }
        int result= userService.userLogout(request);
         return ResultUtils.success(result);
    }

    /**
     *
     * @param request
     * @return
     */
    @GetMapping("/current")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
        Object obj = request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        User currentUser = (User) obj;
        if (currentUser == null) {
            return null;
        }
        Long id = currentUser.getId();
        User user= userService.getById(id);
        return ResultUtils.success(userService.getSafetyUser(user));

    }

    /**
     * 查询用户
     * @param username
     * @param request
     * @return
     */
    @GetMapping("/search")
    public BaseResponse<List<User>> selectUsers(String username, HttpServletRequest request){
        boolean admin = isAdmin(request);
        if(!admin){
           throw new BussinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<User>queryWrapper=new QueryWrapper<>();
        if(!StringUtils.isAnyBlank(username)){
            queryWrapper.like("username",username);
        }
        List<User> userList = userService.list(queryWrapper);
       List<User>list= userList.stream().map(user ->
            userService.getSafetyUser(user)
        ).collect(Collectors.toList());
       return ResultUtils.success(list);

    }

    /**
     * 删除用户
     * @param id
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteUser(Long id,HttpServletRequest request){
        if(!isAdmin(request)){
            throw new BussinessException(ErrorCode.NOT_AUTH);
        }
        if(id<=0){
            throw new BussinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(userService.removeById(id));
    }
   private boolean isAdmin(HttpServletRequest request){
        Object obj=request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
        User user=(User)obj;
        return user!=null&&user.getUserRole()==UserConstant.ADMIN_ROLE;
   }
}

